import createRouteMap from "./create-route-map"
import { createRoute } from './history/base'

export default function createMatcher(routes) {
  /**
   * 最终希望的路由匹配表
   * pathMap: {'/': Home, '/about': About, '/about/a': AboutA, '/about/b': AboutB}
   */

  let pathMap  = createRouteMap(routes, pathMap)  // 扁平化routes配置

  function match(location) {
    let record = pathMap[location]
    if (record) {
      return createRoute(record, {
        path: location
      })
    }
    return createRoute(null, {
      path: location
    })
  }

  // 动态添加路由规则
  function addRoutes(routes) {
    createRouteMap(routes, pathMap)
  }

  return {
    addRoutes, // 动态添加路由规则
    match     // 匹配path渲染的组件
  }
}